package m.jclecai.com.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import m.jclecai.com.bean.BankBean;
import m.jclecai.com.bean.DebugBean;
import m.jclecai.com.bean.HomeItem;
import m.jclecai.com.bean.UiClickBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.utils.http.FastOk;

/**
 * @author 胡业辉
 * @ClassName: PreferenceUtil
 * @Description: SharedPreferences操作工具包
 * @date 2014年12月23日
 */
public class PreferenceUtil {
    public final static String TEXTSETSIZE = "TEXTSIZE";
    public final static String SHAREDPREFERENCES_NAME = "Gxt_app_shared_prefs";
    private static final String SP_UI_CLICK_CACHE = "sc_app_ui_click";
    private static final String SHOW_GUIDE_FIRST = "show_guide_first";

    public static final String KF_NUMBER = "kefuNumber";
    public static final String WEXIN = "weixin";

    public static final String IS_USER_NAME_EMPTY ="isUserNameEmpty";
    public static final String KF_QQ_NUMBER = "kefuQQNumber";
    public static final String INVITE_CODE = "inviteCode";

    public static final String WEI_XIN_IS_BIND_PHONE_NUMBER="weiXinIsBindphoneNumber";


    /**
     * 写入String
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeString(Context context, String key, String value) {
        if (context == null) {
            return ;
        }
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putString(key, value);
        editor.commit();
    }

    /**
     * 写入boolean
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeBoolean(Context context, String key, boolean value) {
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    /**
     * 写入int
     *
     * @param context
     * @param key
     * @param value
     */
    public static void writeInt(Context context, String key, int value) {
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.putInt(key, value);
        editor.commit();
    }


    /**
     * 读取int
     *
     * @param context
     * @param k
     * @param defValue
     * @return
     */
    public static int readInt(Context context, String k, int defValue) {
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        return preference.getInt(k, defValue);
    }


    /**
     * 读取boolean
     *
     * @param context
     * @param k
     * @param defValue
     * @return
     */
    public static boolean readBoolean(Context context, String k, boolean defValue) {
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        return preference.getBoolean(k, defValue);
    }


    /**
     * 读取String
     *
     * @param context
     * @param k
     * @param defV
     * @return
     */
    public static String readString(Context context, String k, String defV) {
        SharedPreferences preference = context.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        return preference.getString(k, defV);
    }

    /**
     * 移除key为k的缓存
     *
     * @param context
     * @param k
     */
    public static void remove(Context context, String k) {
        SharedPreferences preference = context.getSharedPreferences(
                SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        editor.remove(k);
        editor.commit();
    }

    /**
     * 存储userbean
     */
    public static void writeUser(Context context, UserBean userbean) {
        String usb;
        if (userbean == null) {
            usb = "";
            writeBoolean(context, "login_state", false);
        } else
            usb = JSON.toJSONString(userbean);
        writeString(context, "userbean", usb);
    }

    /*
     * 读取userbean
     */
    @SuppressWarnings("static-access")
    public static UserBean readUser(Context context) {
        String usb = readString(context, "userbean", null);
        if (TextUtils.isEmpty(usb)) return null;
        else return FastOk.get(usb, UserBean.class);
    }


    /**
     * 清除所有的缓存
     *
     * @param cxt
     */
    public static boolean cleanPreference(Context cxt) {
        SharedPreferences preference = cxt.getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
        Editor editor = preference.edit();
        return editor.clear().commit();
    }

    //登录时输入的帐号 不是username
    public static String getAccount(Context cxt) {
        return readString(cxt, "phone", "");
    }

    public static void setAccount(Context cxt, String value) {
        writeString(cxt, "phone", value);
    }

    //登录时输入的帐号 不是username
    public static String getUserName(Context cxt) {
        return readString(cxt, "userName", "");
    }

    public static void setUserName(Context cxt, String value) {
        writeString(cxt, "userName", value);
    }

    public static String getPassword(Context cxt) {
        return readString(cxt, "password", "");
    }


    public static void setPassword(Context cxt, String value) {
        writeString(cxt, "password", value);
    }

    public static boolean isLogin(Context cxt) {
        return readBoolean(cxt, "login_state", false);
    }

    public static void setLoginStatus(Context cxt, boolean value) {
        writeBoolean(cxt, "login_state", value);
    }

    public static void clearUserInfo(Context mContext) {
        setAccount(mContext, "");
        setUserName(mContext, "");
        setPassword(mContext, "");
        setLoginStatus(mContext, false);
        writeUser(mContext, null);
    }

    public static UiClickBean getUiClickCache(Context mContext) {
        String string = readString(mContext, SP_UI_CLICK_CACHE, null);
        if (TextUtils.isEmpty(string)) return new UiClickBean();
        else return new Gson().fromJson(string, UiClickBean.class);
    }

    //保存ui的点击操作(例如幸运选号)
    public static void saveUiClickCache(Context mContext, UiClickBean bean) {
        if (bean == null) return;
        writeString(mContext, SP_UI_CLICK_CACHE, new Gson().toJson(bean));
    }

    public static BankBean getDefaultWithDrawBank(Context cxt) {
        String defaultBank = readString(cxt, "defaultBank", null);
        if (TextUtils.isEmpty(defaultBank)) return null;
        Map<String, BankBean> map = FastOk.get(defaultBank, new TypeReference<Map<String, BankBean>>() {
        });
        return map.get(getUserName(cxt));
    }

    public static void setDefaultWithDrawBank(Context cxt, BankBean bean) {
        Map<String, BankBean> map = new HashMap<>();
        map.put(getUserName(cxt), bean);
        String str = JSON.toJSONString(map);
        writeString(cxt, "defaultBank", str);
    }

    public static void clearWithDrawBank(Context cxt) {
        clearWithDrawBank(cxt, getUserName(cxt));
    }

    public static void clearWithDrawBank(Context cxt, String userName) {
        Map<String, BankBean> map = new HashMap<>();
        map.put(userName, null);
        String str = JSON.toJSONString(map);
        writeString(cxt, "defaultBank", str);
    }

    public static DebugBean getDebugBean(Context cxt) {
        String string = readString(cxt, "defaultDebugBean", null);
        if (TextUtils.isEmpty(string)) return new DebugBean();
        else return new Gson().fromJson(string, DebugBean.class);
    }

    public static void setDebugBean(Context mContext, DebugBean bean) {
        if (bean == null) return;
        writeString(mContext, "defaultDebugBean", new Gson().toJson(bean));
    }

    /**
     * 是否显示遗漏
     */
    public static boolean isShowMissing(Context context) {

        return readBoolean(context, "isShowMissing", false);
    }


    /**
     * 设置是否显示遗漏
     *
     * @param context 上下文显示问题；
     * @param isShow  true表示显示，false表示不显示
     */
    public static void setIsShowMissing(Context context, boolean isShow) {
        writeBoolean(context, "isShowMissing", isShow);
    }


    public static boolean isShowGuideFirst(Context cxt) {
        return readBoolean(cxt, SHOW_GUIDE_FIRST, true);
    }

    public static void setShowGuideFirst(Context cxt, boolean bool) {
        writeBoolean(cxt, SHOW_GUIDE_FIRST, bool);
    }

    public static void setHomeMenu(Context mContext, List list) {
        if (list == null) return;
        writeString(mContext, "defaultHomeMenu", new Gson().toJson(list));
    }

    public static List<HomeItem> getHomeMenu(Context mContext) {
        String string = readString(mContext, "defaultHomeMenu", null);
        if (TextUtils.isEmpty(string)) return null;
        else return FastOk.getList(string,HomeItem.class);
    }
}
